package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.service.seckill.SeckillOrderService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/seckill/order")
public class SeckillOrderController {

    @Reference
    private SeckillOrderService seckillOrderService;

    /**
     * 用户下单操作
     *
     * @param id
     * @return
     */
    @GetMapping("/add")
    public Result add(Long id, String time) {
        try {
            String userName = SecurityContextHolder.getContext().getAuthentication().getName();
            //判断用户是否登录
            if (userName.equalsIgnoreCase("anonymousUser")) {
                return new Result(403, "未登录,请先登录");
            }
            Boolean flag = seckillOrderService.add(id, time, userName);
            if (flag) {
                //抢单成功
                return new Result(0, "下单成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, e.getMessage());
        }
        return new Result(1, "下单失败!");
    }
}
